home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / p_misc / 7p203upp.exe / V203SRC.LZH / EXTRACT.C < prev    next >
C/C++ Source or Header  |  1992-06-22  |  5KB  |  197 lines

  1. #include "7plus.h"
  2. #include "globals.h"
  3.  
  4. /*
  5. *** extract 7plus-files from log-file.
  6. ***
  7. ***
  8.  */
  9.  
  10. int extract_files (char *name, char *search)
  11. {
  12.   FILE *in, *out;
  13.   char string[81], destnam[13], dummi[20], *p, *q;
  14.   char *list_top;
  15.  
  16.   int  part, file, err, errn, cor, corn, ret, lines, info, offset;
  17.   ulong bytes, sum;
  18.  
  19.   list_top = "File         Length Lines\n------------ ------ -----\n";
  20.   out = NULLFP;
  21.   file = err = errn = cor = corn = ret = lines = offset = 0;
  22.   info = 1;
  23.   bytes = sum = 0UL;
  24.  
  25.   if (search)
  26.     strlwr (search);
  27.  
  28.   printf ("\n--------------------\n7PLUS file extractor\n--------------------\n");
  29.  
  30.   if (!(in = fopen (name, OPEN_READ_BINARY)))
  31.   {
  32.     printf (cant, name);
  33.     return (2);
  34.   }
  35.   setvbuf (in, NULL, _IOFBF, buflen);
  36.  
  37.   printf ("\nScanning '%s' for 7PLUS-files.\n\n%s", name, list_top);
  38.  
  39.   while ((p = my_fgets (string, 80, in)) != NULL)
  40.   {
  41.     offset = 0;
  42.  
  43.     /* This is necessary, because of some strange BBS in the UK that keeps
  44.        stripping the space in the first line. */
  45.     if (!strncmp (p, " go_", 4))
  46.       offset = 4;
  47.     if (!strncmp (p, "go_", 3))
  48.       offset = 3;
  49.  
  50.     if (offset)
  51.     { /* Beginning of a 7PLUS file found. */
  52.       if (out)
  53.       {
  54.         printf ("%6lu %5d\n", bytes, lines);
  55.         sum += bytes;
  56.         fclose (out);
  57.         out = NULLFP;
  58.       }
  59.       *destnam = EOS;
  60.       if (!strncmp (p+offset, "7+.", 3)) /* It's a code file */
  61.       {
  62.         /* Get filename from header. Create output filename. */
  63.         sscanf (p+offset+3, "%d%s%s%s", &part, dummi, dummi, destnam);
  64.         if ((q = strrchr (destnam, '.')) != NULL)
  65.           *q = EOS;
  66.         destnam[8] = EOS;
  67.         if (strstr (p, "of 001"))
  68.           sprintf (dummi, ".7PL");
  69.         else
  70.           sprintf (dummi, ".P%02x", part);
  71.         strcat (destnam, dummi);
  72.       }
  73.       /* OK, then it could be an ERR or COR file.
  74.          Careful! It could also be a marked textfile */
  75.       if (!strncmp (p+offset, "text.", 5) &&
  76.           (strstr (p, ".ERR") || strstr (p, ".COR")))
  77.         sscanf (p+offset+6, "%12s", destnam);
  78.  
  79.       /* It could also be an info file accompanying the code file */
  80.       if (!strncmp (p+offset, "info.", 5))
  81.       {
  82.         sscanf (p+offset+6, "%12s", destnam);
  83.         info = 0;
  84.       }
  85.       strlwr (destnam);
  86.  
  87.       err = cor = 0;
  88.       if (strstr (p, ".ERR"))
  89.         err = 1;
  90.       if (strstr (p, ".COR"))
  91.         cor = 1;
  92.  
  93.       if (search && *destnam && !strstr (destnam, search))
  94.       {
  95.         printf ("%-12s ------ -----\n", destnam);
  96.         *destnam = EOS;
  97.       }
  98.       if (*destnam) /* Open output file if 7PLUS file found. */
  99.       {
  100.         /* create filename for output file */
  101.         if (err && !test_exist (destnam))
  102.         {
  103.           errn = 1;
  104.           do
  105.           {
  106.             if ((q = strrchr (destnam, '.')) != NULL)
  107.               *q = EOS;
  108.             sprintf (dummi, ".e%02x", errn++);
  109.             strcat (destnam, dummi);
  110.           }
  111.           while (!test_exist (destnam));
  112.         }
  113.  
  114.         if (cor && !test_exist (destnam))
  115.         {
  116.           corn = 1;
  117.           do
  118.           {
  119.             if ((q = strrchr (destnam, '.')) != NULL)
  120.               *q = EOS;
  121.             sprintf (dummi, ".c%02x", corn++);
  122.             strcat (destnam, dummi);
  123.           }
  124.           while (!test_exist (destnam));
  125.         }
  126.  
  127.         file++;
  128.         if (test_file (in, destnam, 1, 12))
  129.           printf (list_top);
  130.         printf ("%-12s ", destnam);
  131.         out = fopen (destnam, OPEN_WRITE_TEXT);
  132.         setvbuf (out, NULL, _IOFBF, buflen);
  133.         bytes = 0UL;
  134.         lines = 0;
  135.       }
  136.     }
  137.  
  138.     if (out)
  139.     {
  140.       /* End of file reached? */
  141.       if (!strncmp (p, " stop_", 6))
  142.       {
  143.         info = 2; /* yes */
  144.         if (!strncmp (p+6, "info.", 5))
  145.         {
  146.           lines--;
  147.           *p = EOS;
  148.         }
  149.       }
  150.  
  151.       if (info)
  152.       {
  153.         if (offset == 3)
  154.           ret = fprintf (out, " ");
  155.  
  156.         ret = fprintf (out, "%s", p);
  157.  
  158.         lines++;
  159.  
  160.         if (ret == EOF)
  161.         {
  162.           printf ("\007\nWrite error. Can't continue. Break.\n");
  163.           exit (1);
  164.         }
  165.  
  166.         #ifdef TWO_CHAR_SEP
  167.          if (p[(int)ret-1] == '\n')
  168.            ret++;
  169.         #endif
  170.         bytes += (ulong) ret;
  171.  
  172.         if (info == 2)
  173.         {
  174.           printf ("%6lu %5d\n", bytes, lines);
  175.           sum += bytes;
  176.           fclose (out);
  177.           out = NULLFP;
  178.         }
  179.       }
  180.     }
  181.     info = 1;
  182.   }
  183.  
  184.   if (file)
  185.   {
  186.     printf ("             ======\n      Total: %6lu\n", sum);
  187.     printf ("\nAll done!\n");
  188.   }
  189.   else
  190.     printf ("No %sfiles found....\n", search?"matching ":"");
  191.  
  192.   fclose (in);
  193.   if (out)
  194.     fclose (out);
  195.   return (0);
  196. }
  197.